package com.vecspace.testhelper.service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vecspace.testhelper.dao.TestHelperDao;
import com.vecspace.utils.AppPropUtil;
import com.vecspace.utils.FileHelper;

@Service("test_testHelperService")
public class TestHelperService {

	private void checkVliad() throws Exception {
		String debugFlag = AppPropUtil.getProperty("app.debug");
		if(!"true".equals(debugFlag)) {
			throw new Exception("app.debug == false 环境下，不能使用TestHelperService");
		}
	}
	/**
	 * 查询一条记录
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public Map<String, Object> queryRow(String sql, Object[] params) throws Exception{
		checkVliad();
		return testHelperDao.queryRow(sql, params);
	}
	
	/**查询多条记录
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public List<Map<String, Object>> queryRows(String sql, Object[] params) throws Exception{
		checkVliad();
		return testHelperDao.queryRows(sql, params);
	}
	
	/**执行insert，update或者delete语句
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public int executeSql(String sql , Object[] params) throws Exception {
		checkVliad();
		return testHelperDao.executeSql(sql, params);
	}
		
	public int executeSqlFile(String sqlFileName) throws Exception {
		checkVliad();
		String filePath = "app_test_sql" + File.separatorChar + sqlFileName;
		String sqlText = FileHelper.readFileText(filePath);
		int sqlCount = 0;
		if(sqlText != null && sqlText.length() > 10) {
			String[] sqlArr = sqlText.split("\n");
			for(String sql : sqlArr) {
				if(sql == null || sql.trim().length() < 5)continue;
				sql = sql.trim();
				if(sql.startsWith("--"))continue;
				if(sql.startsWith("/*"))continue;
				if(sql.endsWith(";"))sql = sql.substring(0, sql.length() - 1);
				testHelperDao.executeSql(sql, null);
				++sqlCount;
			}
		}
		return sqlCount;
	}
	/**可以为空
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public String getString(String sql, Object[] params) throws Exception {
		checkVliad();
		return testHelperDao.getString(sql, params);
	}
	

	/**
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public int getInt(String sql, Object[] params) throws Exception {
		checkVliad();
		return testHelperDao.getInt(sql, params);
	}
	

	/**
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public Date getDate(String sql, Object[] params) throws Exception {
		checkVliad();
		return testHelperDao.getDate(sql, params);
	}
	

	/**
	 * 
	 * @param sql
	 * @param params 可以为null
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public long getLong(String sql, Object[] params) throws Exception {
		checkVliad();
		return testHelperDao.getLong(sql, params);
	}
	
	@Autowired
	private TestHelperDao testHelperDao;
}
